<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="机构ID" prop="infoId">
        <el-input v-model="queryParams.infoId" placeholder="请输入机构ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="机构名称" prop="infoName">
        <el-input v-model="queryParams.infoName" placeholder="请输入机构名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="日期" prop="dateInfo">
        <el-date-picker clearable v-model="queryParams.dateInfo" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="noticeList">
      <el-table-column label="日期" align="center" prop="dateInfo" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dateInfo, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="机构ID" align="center" prop="infoId" />
      <el-table-column label="机构名称" align="center" prop="infoName" />
      <el-table-column label="通知类型" align="center" prop="noticeType" />
      <el-table-column label="请求uri" align="center" prop="uri" />
      <el-table-column label="form 参数" align="center" prop="paramMap">
        <template slot-scope="scope">
          <el-tooltip :content="scope.row.paramMap" placement="top-start"
            :disabled="!scope.row.paramMap || scope.row.paramMap.length <= 20">
            <div v-if="scope.row.paramMap">{{ scope.row.paramMap.length <= 20 ? scope.row.paramMap :
              scope.row.paramMap.slice(0, 20) + '...' }}</div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="json 参数" align="center" prop="resultInfo">
        <template slot-scope="scope">
          <el-tooltip :content="scope.row.paramContent" placement="top-start"
            :disabled="!scope.row.paramContent || scope.row.paramContent.length <= 20">
            <div v-if="scope.row.paramContent">{{ scope.row.paramContent.length <= 20 ? scope.row.paramContent :
              scope.row.paramContent.slice(0, 20) + '...' }}</div>
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column label="返回值" align="center" prop="returnResult">
        <template slot-scope="scope">
          <el-tooltip :content="scope.row.returnResult" placement="top-start"
            :disabled="!scope.row.returnResult || scope.row.returnResult.length <= 20">
            <div v-if="scope.row.returnResult">{{ scope.row.returnResult.length <= 20 ? scope.row.returnResult :
              scope.row.returnResult.slice(0, 20) + '...' }}</div>
          </el-tooltip>
        </template>
      </el-table-column>


      <el-table-column label="创建时间" align="center" width="150">
        <template slot-scope="scope">
          {{ formatTimestamp(scope.row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="small" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['merchant:notice:edit']">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改商户回调通知日录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      
        <el-form-item label="请求uri" prop="uri">
          <el-input v-model="form.uri"/>
        </el-form-item>
        <el-form-item label="form 参数" prop="paramMap">
          <el-input v-model="form.paramMap" type="textarea" />
        </el-form-item>
        <el-form-item label="json 参数">
          <el-input v-model="form.paramContent" type="textarea"  />
        </el-form-item>
        <el-form-item label="返回值" prop="returnResult">
          <el-input v-model="form.returnResult" type="textarea"  />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listNotice, getNotice, delNotice, addNotice, updateNotice } from "@/api/merchant/notice";

export default {
  name: "Notice",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商户回调通知日录表格数据
      noticeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        infoId: null,
        infoName: null,
        dateInfo: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /**更改创建时间格式 */
    formatTimestamp(timestamp) {
      if (!timestamp) {
        return '';
      }
      const date = new Date(timestamp);
      const formattedDate = `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}:${date.getSeconds().toString().padStart(2, '0')}`;
      return formattedDate;
    },
    /** 查询商户回调通知日录列表 */
    getList() {
      this.loading = true;
      listNotice(this.queryParams).then(response => {
        this.noticeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        infoId: null,
        infoName: null,
        noticeType: null,
        uri: null,
        paramMap: null,
        paramContent: null,
        returnResult: null,
        createTime: null,
        dateInfo: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商户回调通知日录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getNotice(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "商户回调通知日录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateNotice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNotice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商户回调通知日录编号为"' + ids + '"的数据项？').then(function () {
        return delNotice(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('merchant/notice/export', {
        ...this.queryParams
      }, `notice_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
